<template>
    <div style="height: 100vh; display: flex; align-items: center; justify-content: center; background-color: #0f9876;">
      <div style="display: flex; background-color: white; width: 50%; border-radius: 5px; overflow: hidden;">
        <div style="flex: 1;">
          <img src="@/assets/logo.jpg" alt="" style="width: 100%; justify-content: center;">
        </div>
        <div style="flex: 1; display: flex; align-items: center;">
          <el-form :model="admin" style="width: 85%;" :rules="rules" ref="registerRef">
            <div style="font-size: 20px; font-weight: bold; text-align: center;">欢迎注册账户</div>
            <el-form-item prop="username" style="margin-top: 20px;">
              <el-input prefix-icon="el-icon-user" size="medium" placeholder="请输入账号" v-model="admin.username"></el-input>
            </el-form-item>
            <el-form-item prop="name">
              <el-input prefix-icon="el-icon-edit" size="medium" placeholder="请输入姓名" v-model="admin.name"></el-input>
            </el-form-item>
            <el-form-item prop="phone">
              <el-input prefix-icon="el-icon-phone" size="medium" placeholder="请输入电话" v-model="admin.phone"></el-input>
            </el-form-item>
            <el-form-item prop="sex">
              <el-select v-model="admin.sex" placeholder="请选择性别" size="medium" prefix-icon="el-icon-s-custom">
                <el-option label="男" value="男"></el-option>
                <el-option label="女" value="女"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="idNumber">
              <el-input prefix-icon="el-icon-edit" size="medium" placeholder="请输入身份证号" v-model="admin.idNumber"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" style="width: 100%;" @click="register">注 册</el-button>
            </el-form-item>
            <div style="display: flex;">
              <div style="flex: 1;">已有账号？请 <span style="color:#0f9876; cursor: pointer;" @click="login">登录</span></div>
            </div>
          </el-form>
        </div>
      </div>
    </div>
  </template>
  
  <script>
  import request from '@/utils/request'
  export default {
    name: "Register",
    data() {
      return {
        admin: {
          username: '',
          name: '',
          phone: '',
          sex: '',
          idNumber: ''
        },
        rules: {
          username: [
            { required: true, message: '请输入账号', trigger: 'blur' },
            { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '请输入姓名', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '请输入电话', trigger: 'blur' },
            { pattern: /^1[3-9]\d{9}$/, message: '请输入有效的电话号码', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '请选择性别', trigger: 'blur' }
          ],
          idNumber: [
            { required: true, message: '请输入身份证号', trigger: 'blur' },
            { pattern: /^[1-9]\d{5}(18|19|20)?\d{2}((0[1-9])|(10|11|12))(([0-2][0-9])|10|20|30|31)\d{3}(\d|[Xx])$/, message: '请输入有效的身份证号', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      register() {
        this.$refs['registerRef'].validate((valid) => {
          if (valid) {
            // 验证通过
            request.post('/admin/admin/create', this.admin).then(res => {
              console.log(res.code)
              if (res.code === 1) {
                this.$message.success('注册成功')
                this.$router.push('/login')
              } else {
                this.$message.error('注册失败')
              }
            })
          }
        })
      },
      login() {
        this.$router.push('/login')
      }
    }
  }
  </script>
  
  <style>
  </style>